//
//  tableVC.m
//  Notification
//
//  Created by Apple on 2017/3/22.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "tableVC.h"
#import "TableViewCell.h"
@interface tableVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation tableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"TableVC";
    self.view.backgroundColor = [UIColor whiteColor];
    self.parray = [NSMutableArray array];
    self.array = [NSMutableArray arrayWithObjects:@"过河拆桥",@"无中生有",@"无懈可击",@"五谷丰登",@"桃园结义",@"顺手牵羊",@"南蛮入侵",@"万箭齐发",@"决斗", nil];
    for (int i = 0; i<9; i++) {
        NSString*str = [NSString stringWithFormat:@"picture%d.jpg",i];
        [self.parray addObject:str];
    }
    self.table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor whiteColor];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table registerClass:[TableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:self.table];

#pragma mark-设置右上角编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
#pragma mark-设置其为可编辑状态
    [self.table setEditing:YES animated:YES];
    [super setEditing:editing animated:animated];
#pragma mark-编辑方法，会在edit和done之间切换
    [self.table setEditing:editing animated:animated];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
#pragma mark-设置单元格可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#pragma mark-设置编辑按钮样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
#pragma mark-设置怎么移动 
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
#pragma mark-取得原来数据
    NSString*nam = self.array[sourceIndexPath.row];
#pragma mark-移除
    [self.array removeObject:nam];
#pragma mark-添加
    [self.array insertObject:nam atIndex:destinationIndexPath.row];
}
#pragma mark-设置有多少分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark-设置删除方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
      [self.array removeObjectAtIndex:indexPath.row];
      [self.parray removeObjectAtIndex:indexPath.row];
      [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
      [tableView reloadData];
    }
}
#pragma mark-设置每个分区有多少cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
#pragma mark-设置cell的类型
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    cell.lab.text = self.array[indexPath.row];
    cell.lab.textAlignment = NSTextAlignmentCenter;
    cell.ima.image = [UIImage imageNamed:self.parray[indexPath.row]];
    return cell;
}
#pragma mark-设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}
#pragma mark-设置分区标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"第一分区";
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
